package components;

import java.io.File;
import java.io.IOException;
import java.util.Locale;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class ExcelWriter {

	private int row, col;
	private WritableWorkbook workbook;
	private WritableSheet sheet;

	public ExcelWriter(File file) throws IOException {
		WorkbookSettings wbSettings = new WorkbookSettings();

		wbSettings.setLocale(new Locale("en", "EN"));

		workbook = Workbook.createWorkbook(file, wbSettings);
		workbook.createSheet("Report", 0);
		sheet = workbook.getSheet(0);
	}

	public void setRow(int row) {
		this.row = row;
	}

	public void setColumn(int col) {
		this.col = col;
	}

	public void enter() {
		this.row ++;
	}

	public void writeNumber(double num) throws RowsExceededException, WriteException {
		Number number = new Number(col, row, num);
		sheet.addCell(number);
	}

	public void writeNumber(int row, int col, double num) throws RowsExceededException, WriteException {
		this.row = row;
		this.col = col;
		writeNumber(num);
	}

	public void saveDocument() throws IOException, WriteException {
		workbook.write();
	}
	
	public void closeDocument() throws WriteException, IOException {
		workbook.close();
	}

	public static void main(String[] args) throws WriteException, IOException {
		ExcelWriter test = new ExcelWriter(new File("/Users/Camilo/Desktop/simulation/Connectivity.xls"));
		test.writeNumber(44);
		test.enter();
		test.writeNumber(45);
		test.enter();
		test.writeNumber(46);
		test.saveDocument();
	}
}